package com.smartleanx.module.hrm.service.examine.examineResultConfig;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineResultConfig.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResultConfig.ExamineResultConfigDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineResultConfig.ExamineResultConfigMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核结果设置项	 Service 实现类
 *
 * @author
 */
@Service
@Validated
public class ExamineResultConfigServiceImpl implements ExamineResultConfigService {

    @Resource
    private ExamineResultConfigMapper examineResultConfigMapper;

    @Override
    public Integer createExamineResultConfig(ExamineResultConfigSaveReqVO createReqVO) {

        // 插入
        ExamineResultConfigDO examineResultConfig = BeanUtils.toBean(createReqVO, ExamineResultConfigDO.class);
        examineResultConfigMapper.insert(examineResultConfig);
        // 返回
        return examineResultConfig.getId();
    }

    @Override
    public void updateExamineResultConfig(ExamineResultConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineResultConfigExists(updateReqVO.getId());
        // 更新
        ExamineResultConfigDO updateObj = BeanUtils.toBean(updateReqVO, ExamineResultConfigDO.class);
        examineResultConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineResultConfig(Integer id) {
        // 校验存在
        validateExamineResultConfigExists(id);
        // 删除
        examineResultConfigMapper.deleteById(id);
    }

    private void validateExamineResultConfigExists(Integer id) {
        if (examineResultConfigMapper.selectById(id) == null) {
            throw exception(EXAMINE_RESULT_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ExamineResultConfigDO getExamineResultConfig(Integer id) {
        return examineResultConfigMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineResultConfigDO> getExamineResultConfigPage(ExamineResultConfigPageReqVO pageReqVO) {
        return examineResultConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<JSONObject> selectTableList(Integer examineId) {
        List<ExamineResultConfigDO> examineResultConfigDOS = examineResultConfigMapper.selectList(new LambdaQueryWrapper<ExamineResultConfigDO>(){{
            eq(ExamineResultConfigDO::getExamineId, examineId);
        }});
        List<JSONObject> reslutList = new ArrayList<>();
        JSONObject all = new JSONObject();
        all.put("name", "全部");
        all.put("value", 0);
        reslutList.add(all);
        examineResultConfigDOS.forEach(examineResultConfigDO -> {
            JSONObject result = new JSONObject();
            result.put("name", examineResultConfigDO.getGradeName());
            result.put("value", examineResultConfigDO.getId());
            reslutList.add(result);
        });
//        JSONObject unrated = new JSONObject();
//        unrated.put("name", "未定级");
//        unrated.put("value", -1);
//        reslutList.add(unrated);
        return reslutList;
    }

}